<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs">
<head>
<title>函数声明</title>
<meta charset="UTF-8">
<meta name="generator" content="MediaWiki 1.21.2">
<link rel="shortcut icon" href="../../../common/favicon.ico">
<link rel="stylesheet" href="../../../common/ext.css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" href="../../../common/site_modules.css">
<style>a:lang(ar),a:lang(ckb),a:lang(fa),a:lang(kk-arab),a:lang(mzn),a:lang(ps),a:lang(ur){text-decoration:none}#toc{display:none}.editsection{display:none}
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-css:7:15cea3ec788a65b5187d4018eed543bf */</style>

<script src="../../../common/startup_scripts.js"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"c/language/function_declaration","wgTitle":"c/language/function declaration","wgCurRevisionId":69015,"wgArticleId":7970,"wgIsArticle":true,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":[],"wgBreakFrames":false,"wgPageContentLanguage":"zh","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"zh","wgMonthNames":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgMonthNamesShort":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgRelevantPageName":"c/language/function_declaration","wgUserVariant":"zh","wgRestrictionEdit":[],"wgRestrictionMove":[]});
}</script><script>if(window.mw){
mw.loader.implement("user.options",function(){mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":0,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":0,"showjumplinks":1,"shownumberswatching":1,"showtoc":0,"showtoolbar":1,"skin":"cppreference2","stubthreshold":0,"thumbsize":2,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":0,"watchdefault":0,"watchdeletion":0,
"watchlistdays":3,"watchlisthideanons":0,"watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"variant":"zh","language":"zh","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false,"gadget-MathJax":1,"gadget-ColiruCompiler":1});;},{},{});mw.loader.implement("user.tokens",function(){mw.user.tokens.set({"editToken":"+\\","patrolToken":false,"watchToken":false});;},{},{});
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-js:7:258d7cd6aa9aa67dee25e01fb6a9e505 */
}</script>
<script>if(window.mw){
mw.loader.load(["mediawiki.page.startup","mediawiki.legacy.wikibits","mediawiki.legacy.ajax"]);
}</script>
<style type="text/css">/*<![CDATA[*/
.source-c {line-height: normal;}
.source-c li, .source-c pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for c
 * CSS class: source-c, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.c.source-c .de1, .c.source-c .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.c.source-c  {font-family:monospace;}
.c.source-c .imp {font-weight: bold; color: red;}
.c.source-c li, .c.source-c .li1 {font-weight: normal; vertical-align:top;}
.c.source-c .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.c.source-c .li2 {font-weight: bold; vertical-align:top;}
.c.source-c .kw1 {color: #0000dd;}
.c.source-c .kw2 {color: #0000ff;}
.c.source-c .kw3 {color: #0000dd;}
.c.source-c .kw4 {color: #0000ff;}
.c.source-c .co1 {color: #909090;}
.c.source-c .co2 {color: #339900;}
.c.source-c .coMULTI {color: #ff0000; font-style: italic;}
.c.source-c .es0 {color: #008000; font-weight: bold;}
.c.source-c .es1 {color: #008000; font-weight: bold;}
.c.source-c .es2 {color: #008000; font-weight: bold;}
.c.source-c .es3 {color: #008000; font-weight: bold;}
.c.source-c .es4 {color: #008000; font-weight: bold;}
.c.source-c .es5 {color: #008000; font-weight: bold;}
.c.source-c .br0 {color: #008000;}
.c.source-c .sy0 {color: #008000;}
.c.source-c .sy1 {color: #000080;}
.c.source-c .sy2 {color: #000040;}
.c.source-c .sy3 {color: #000040;}
.c.source-c .sy4 {color: #008080;}
.c.source-c .st0 {color: #008000;}
.c.source-c .nu0 {color: #000080;}
.c.source-c .nu6 {color:#000080;}
.c.source-c .nu8 {color:#000080;}
.c.source-c .nu12 {color:#000080;}
.c.source-c .nu16 {color:#000080;}
.c.source-c .nu17 {color:#000080;}
.c.source-c .nu18 {color:#000080;}
.c.source-c .nu19 {color:#000080;}
.c.source-c .ln-xtra, .c.source-c li.ln-xtra, .c.source-c div.ln-xtra {background-color: #ffc;}
.c.source-c span.xtra { display:block; }

/*]]>*/
</style><!--[if lt IE 7]><style type="text/css">body{behavior:url("/mwiki/skins/cppreference2/csshover.min.htc")}</style><![endif]--></head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-c_language_function_declaration skin-cppreference2 action-view cpp-navbar">
        <!-- header -->
        <!-- /header -->
        <!-- content -->
<div id="cpp-content-base">
            <div id="content">
                <a id="top"></a>
                <div id="mw-js-message" style="display:none;"></div>
                                <!-- firstHeading -->
<h1 id="firstHeading" class="firstHeading">函数声明</h1>
                <!-- /firstHeading -->
                <!-- bodyContent -->
                <div id="bodyContent">
                                        <!-- tagline -->
                    <div id="siteSub">来自cppreference.com</div>
                    <!-- /tagline -->
                                        <!-- subtitle -->
                    <div id="contentSub"><span class="subpages">&lt; <a href="../../c.html" title="c">c</a>‎ | <a href="../language.html" title="c/language">language</a></span></div>
                    <!-- /subtitle -->
                                                            <!-- bodycontent -->
                    <div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr"><div class="t-navbar" style=""><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../../c.html" title="c"> C</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv"><td colspan="5"> <a href="../language.html" title="c/language">语言</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../header.html" title="c/header">头文件</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../types.html" title="c/types">类型支持</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../program.html" title="c/program">程序工具</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../variadic.html" title="c/variadic">变参数函数支持</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../memory.html" title="c/memory">动态内存管理</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../error.html" title="c/error">错误处理</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../chrono.html" title="c/chrono">日期和时间工具</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../string.html" title="c/string">字符串库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../algorithm.html" title="c/algorithm">算法</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../numeric.html" title="c/numeric">数值</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../io.html" title="c/io">输入/输出支持</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../locale.html" title="c/locale">本地化支持</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../thread.html" title="c/thread">线程支持</a> <span class="t-mark-rev t-since-c11">(C11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../atomic.html" title="c/atomic">原子操作</a> <span class="t-mark-rev t-since-c11">(C11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../experimental.html" title="c/experimental">技术规范</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../language.html" title="c/language">C 语言</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv-h2"><td colspan="5"> <a href="basic_concepts.html" title="c/language/basic concepts">基本概念</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="../keyword.html" title="c/keyword">关键词</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="../preprocessor.html" title="c/preprocessor">预处理器</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="statements.html" title="c/language/statements">语句</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="expressions.html" title="c/language/expressions">表达式</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="initialization.html" title="c/language/initialization">初始化</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="declarations.html" title="c/language/declarations">声明</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="functions.html" title="c/language/functions">函数</a></td></tr>
<tr class="t-nv"><td colspan="5"> 杂项</td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="history.html" title="c/language/history">C 的历史</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="../experimental.html" title="c/experimental">技术规范</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="functions.html" title="c/language/functions">函数</a><div class="t-navbar-menu"><div><div style="display:inline-block">
<div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv"><td colspan="5"> <strong class="selflink">函数声明</strong></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="function_definition.html" title="c/language/function definition">函数定义</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="variadic.html" title="c/language/variadic">可变参数</a></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="inline.html" title="c/language/inline"><span class="t-lines"><span>inline</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="_Noreturn.html" title="c/language/ Noreturn"><span class="t-lines"><span>_Noreturn</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div></div></td></tr>
</table></div>
</div><div></div></div></div></div><div class="t-navbar-sep"> </div></div>
<p>函数声明引入指代函数的<a href="identifier.html" title="c/language/identifier">标识符</a>，并可选地指定该函数的参数类型（<i>原型</i>）。函数声明（不同于<a href="function_definition.html" title="c/language/function definition">定义</a>）可以出现于块作用域和文件作用域中。
</p>
<h3><span class="mw-headline" id=".E8.AF.AD.E6.B3.95">语法</span></h3>
<p>在函数声明的<a href="declarations.html" title="c/language/declarations">声明文法</a>中， <span class="t-spar">类型说明符</span> 序列，可选择地由声明器修饰，指代<i>返回类型</i>（可以是任何异于函数和数组的类型），而 <span class="t-spar">声明符</span> 有三种形式：
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">非指针声明符</span>  <code><b>(</b></code> <span class="t-spar">形参列表</span> <code><b>)</b></code> <span class="t-spar">属性说明符序列</span><span class="t-mark">(可选)</span>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">非指针声明符</span>  <code><b>(</b></code> <span class="t-spar">标识符列表</span> <code><b>)</b></code> <span class="t-spar">属性说明符序列</span><span class="t-mark">(可选)</span>
</td>
<td> (2)
</td>
<td> <span class="t-mark-rev t-until-c23">(C23 前)</span>
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">非指针声明符</span>  <code><b>(</b></code> <code><b>)</b></code> <span class="t-spar">属性说明符序列</span><span class="t-mark">(可选)</span>
</td>
<td> (3)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<p>其中
</p>
<table class="t-par-begin">


<tr class="t-par">
<td> <span class="t-spar">非指针声明符</span>
</td>
<td> -
</td>
<td>  任何无括号指针声明器以外的<a href="declarations.html#.E5.A3.B0.E6.98.8E.E5.99.A8" title="c/language/declarations">声明器</a>。包含于此声明器中的标识符是成为函数指代器的标识符。
</td></tr>
<tr class="t-par">
<td> <span class="t-spar">形参列表</span>
</td>
<td> -
</td>
<td>  为单一关键词 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">void</span></span></span> 或<i>参数</i>的逗号分隔列表，可以以<a href="variadic.html" title="c/language/variadic">省略号参数</a>结尾。
</td></tr>
<tr class="t-par">
<td> <span class="t-spar">标识符列表</span>
</td>
<td> -
</td>
<td>  标识符的逗号分隔列表，仅在声明器被用作旧式<a href="function_definition.html" title="c/language/function definition">函数定义</a>的一部分的情况下可能
</td></tr>
<tr class="t-par">
<td> <span class="t-spar">属性说明符序列</span>
</td>
<td> -
</td>
<td>  <span class="t-mark-rev t-since-c23">(C23)</span>可选的<a href="attributes.html" title="c/language/attributes">属性</a>列表，应用到函数类型
</td></tr></table>
<div class="t-li1"><span class="t-li">1)</span> 新式 (C89) 函数声明。此声明不仅引入函数指代器自身，而且还为任何将来的<a href="operator_other.html#.E5.87.BD.E6.95.B0.E8.B0.83.E7.94.A8" title="c/language/operator other">函数调用表达式</a>提供函数原型，强制将使用参数表达式转换成声明参数类型，还有编译时的参数数量检查。
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw4">int</span> max<span class="br0">(</span><span class="kw4">int</span> a, <span class="kw4">int</span> b<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 声明</span>
<span class="kw4">int</span> n <span class="sy1">=</span> max<span class="br0">(</span><span class="nu16">12.01</span>, <span class="nu16">3.14</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// OK ：从 double 转换到 int</span></pre></div></div></div>
<div class="t-li1"><span class="t-li">2)</span> <span class="t-mark-rev t-until-c23">(C23 前)</span> 旧式 (K&amp;R) 函数定义。此声明不引入函数原型，且任何将来的<a href="operator_other.html#.E5.87.BD.E6.95.B0.E8.B0.83.E7.94.A8" title="c/language/operator other">函数调用表达式</a>都将进行默认参数提升，而且若实参数量不匹配形参数量则引起未定义行为。
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw4">int</span> max<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>
<span class="kw4">int</span> max<span class="br0">(</span>a, b<span class="br0">)</span> 
<span class="kw4">int</span> a, b<span class="sy4">;</span> <span class="br0">{</span> <span class="kw1">return</span> a<span class="sy1">&gt;</span>b<span class="sy4">?</span>a<span class="sy4">:</span>b<span class="sy4">;</span> <span class="br0">}</span> <span class="co1">// 定义期待 int ；第二个调用是未定义行为</span>
<span class="kw4">int</span> n <span class="sy1">=</span> max<span class="br0">(</span><span class="kw2">true</span>, <span class="br0">(</span><span class="kw4">char</span><span class="br0">)</span><span class="st0">'a'</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 以二个 int 参数调用 max （提升后）</span>
<span class="kw4">int</span> n <span class="sy1">=</span> max<span class="br0">(</span><span class="nu17">12.01f</span>, <span class="nu16">3.14</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 以二个 double 参数调用 max （提升后）</span></pre></div></div></div>
<div class="t-li1"><span class="t-li">3)</span> 无原型函数声明。此声明不引入原型<span class="t-rev-inl t-since-c23"><span>，除非作为函数定义的一部分，其中它等价于 <span class="t-spar">形参列表</span> <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">void</span></span></span> </span> <span><span class="t-mark-rev t-since-c23">(C23 起)</span></span></span>。</div>
<h3><span class="mw-headline" id=".E8.A7.A3.E9.87.8A">解释</span></h3>
<p>函数的返回类型，由 <span class="t-spar">说明符与限定符</span> 中的类型说明符确定，并且像在<a href="declarations.html" title="c/language/declarations">声明</a>中一样可以由 <span class="t-spar">声明符</span> 修改，它必须是非数组对象类型或类型 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">void</span></span></span> 。若该函数声明不是定义，则返回类型可以为<a href="type.html#.E4.B8.8D.E5.AE.8C.E6.95.B4.E7.B1.BB.E5.9E.8B" title="c/language/type" class="mw-redirect">不完整</a>。返回类型不能有 cvr 限定：为构造函数类型的目的，调整任何有限定返回类型为其无限定版本。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">char</span> <span class="sy2">*</span>s<span class="br0">)</span><span class="sy4">;</span>                    <span class="co1">// 返回类型为 void</span>
<span class="kw4">int</span> sum<span class="br0">(</span><span class="kw4">int</span> a, <span class="kw4">int</span> b<span class="br0">)</span><span class="sy4">;</span>              <span class="co1">// sum 的返回类型为 int.</span>
<span class="kw4">int</span> <span class="br0">(</span><span class="sy2">*</span>foo<span class="br0">(</span><span class="kw4">const</span> <span class="kw4">void</span> <span class="sy2">*</span>p<span class="br0">)</span><span class="br0">)</span><span class="br0">[</span><span class="nu0">3</span><span class="br0">]</span><span class="sy4">;</span>       <span class="co1">// 返回类型是指向 3 个 int 的数组的指针</span>
 
<span class="kw4">double</span> <span class="kw4">const</span> bar<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span><span class="sy4">;</span>             <span class="co1">// 声明 double(void) 类型函数</span>
<span class="kw4">double</span> <span class="br0">(</span><span class="sy2">*</span>barp<span class="br0">)</span><span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span> <span class="sy1">=</span> bar<span class="sy4">;</span>         <span class="co1">// OK ： foop 是指向 double(void) 的指针</span>
<span class="kw4">double</span> <span class="kw4">const</span> <span class="br0">(</span><span class="sy2">*</span>barpc<span class="br0">)</span><span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span> <span class="sy1">=</span> barp<span class="sy4">;</span> <span class="co1">// OK ： foopc 亦为指向 double(void) 的指针</span></pre></div></div>
<p>函数声明器可以与其他声明器联合，只要他们共享其类型说明符和限定符。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw4">int</span> f<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span>, <span class="sy2">*</span>fip<span class="br0">(</span><span class="br0">)</span>, <span class="br0">(</span><span class="sy2">*</span>pfi<span class="br0">)</span><span class="br0">(</span><span class="br0">)</span>, <span class="sy2">*</span>ap<span class="br0">[</span><span class="nu0">3</span><span class="br0">]</span><span class="sy4">;</span> <span class="co1">// 声明二个函数和二个对象</span>
<span class="kw1">inline</span> <span class="kw4">int</span> g<span class="br0">(</span><span class="kw4">int</span><span class="br0">)</span>, n<span class="sy4">;</span> <span class="co1">// 错误： inline 说明符仅用于函数</span>
<span class="kw1">typedef</span> <span class="kw4">int</span> array_t<span class="br0">[</span><span class="nu0">3</span><span class="br0">]</span><span class="sy4">;</span>
array_t a, h<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 错误：数组类型不能作为函数返回类型</span></pre></div></div>
<p>若函数声明器出现于任何函数外，则其引入的标识符拥有<a href="scope.html" title="c/language/scope">文件作用域</a>和<a href="storage_duration.html" title="c/language/storage duration">外部链接</a>，除非使用 <code>static</code> 或较前的 static 声明可见。若声明出现于另一函数内，则标识符拥有块作用域（且亦拥有外部或内部链接）。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw4">int</span> main<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span>
<span class="br0">{</span>
    <span class="kw4">int</span> f<span class="br0">(</span><span class="kw4">int</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 外部链接，文件作用域</span>
    f<span class="br0">(</span><span class="nu0">1</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 定义需要程序的某处可用</span>
<span class="br0">}</span></pre></div></div>
<p><span class="t-rev-inl t-until-c23"><span>不是<a href="function_definition.html" title="c/language/function definition">函数定义</a>的一部分的声明中，</span> <span><span class="t-mark-rev t-until-c23">(C23 前)</span></span></span>参数不需要命名：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw4">int</span> f<span class="br0">(</span><span class="kw4">int</span>, <span class="kw4">int</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 声明</span>
<span class="co1">// int f(int, int) { return 7; } // 错误，参数在定义中必须命名</span>
<span class="co1">// C23 中允许此定义。</span></pre></div></div>
<p><span class="t-spar">形参列表</span> 中的每个参数是引入单个变量的<a href="declarations.html" title="c/language/declarations">声明</a>，变量拥有下列额外属性：
</p>
<ul><li> 声明器中的标识符是可选的<span class="t-rev-inl t-until-c23"><span>（除非此函数声明是函数定义的一部分）</span> <span><span class="t-mark-rev t-until-c23">(C23 前)</span></span></span>
</li></ul>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw4">int</span> f<span class="br0">(</span><span class="kw4">int</span>, <span class="kw4">double</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// OK</span>
<span class="kw4">int</span> g<span class="br0">(</span><span class="kw4">int</span> a, <span class="kw4">double</span> b<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 也 OK</span>
<span class="kw4">int</span> f<span class="br0">(</span><span class="kw4">int</span>, <span class="kw4">double</span><span class="br0">)</span> <span class="br0">{</span> <span class="kw1">return</span> <span class="nu0">1</span><span class="sy4">;</span> <span class="br0">}</span> <span class="co1">// 错误：定义必须命名参数</span>
<span class="co1">// C23 中允许此定义。</span></pre></div></div>
<ul><li> 仅有的得到允许的<a href="storage_duration.html" title="c/language/storage duration">存储类说明符</a>是 <code>register</code> ，而在非定义的函数声明中忽略它
</li></ul>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw4">int</span> f<span class="br0">(</span><span class="kw4">static</span> <span class="kw4">int</span> x<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 错误</span>
<span class="kw4">int</span> f<span class="br0">(</span><span class="kw4">int</span> <span class="br0">[</span><span class="kw4">static</span> <span class="nu0">10</span><span class="br0">]</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// OK （数组下标的 static 不是存储类说明符）</span></pre></div></div>
<ul><li> 任何数组类型的参数都被调整到对应的指针类型<span class="t-rev-inl t-since-c99"><span>，若数组声明器的方括号内有限定符，则它具有限定</span> <span><span class="t-mark-rev t-since-c99">(C99 起)</span></span></span>。
</li></ul>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw4">int</span> f<span class="br0">(</span><span class="kw4">int</span><span class="br0">[</span><span class="br0">]</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 声明 int f(int*)</span>
<span class="kw4">int</span> g<span class="br0">(</span><span class="kw4">const</span> <span class="kw4">int</span><span class="br0">[</span><span class="nu0">10</span><span class="br0">]</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 声明 int g(const int*)</span>
<span class="kw4">int</span> h<span class="br0">(</span><span class="kw4">int</span><span class="br0">[</span><span class="kw4">const</span> <span class="kw4">volatile</span><span class="br0">]</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 声明 int h(int * const volatile)</span>
<span class="kw4">int</span> x<span class="br0">(</span><span class="kw4">int</span><span class="br0">[</span><span class="sy2">*</span><span class="br0">]</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 声明 int x(int*)</span></pre></div></div>
<ul><li> 任何函数类型的参数都被调整到对应的指针类型
</li></ul>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw4">int</span> f<span class="br0">(</span><span class="kw4">char</span> g<span class="br0">(</span><span class="kw4">double</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 声明 int f(char (*g)(double))</span>
<span class="kw4">int</span> h<span class="br0">(</span><span class="kw4">int</span><span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 声明 int h(int (*)(void))</span></pre></div></div>
<ul><li> 参数列表可以以 <code><b>, ...</b></code> 终止，细节见<a href="variadic.html" title="c/language/variadic">变参数函数</a>。
</li></ul>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw4">int</span> f<span class="br0">(</span><span class="kw4">int</span>, ...<span class="br0">)</span><span class="sy4">;</span></pre></div></div>
<ul><li> 参数不能拥有 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">void</span></span></span> 类型（但可以拥有指向 void 指针类型）。完全由关键词 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">void</span></span></span> 组成的特殊参数列表用于声明不接收参数的函数。
</li></ul>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw4">int</span> f<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// OK</span>
<span class="kw4">int</span> g<span class="br0">(</span><span class="kw4">void</span> x<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 错误</span></pre></div></div>
<ul><li> 任何出现于参数列表中，能被当成 typedef 名或参数名的标识符，都会被当做 typedef 名： <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">int</span> f<span class="br0">(</span><a href="../types/size_t.html"><span class="kw100">size_t</span></a>, <a href="../types/integer.html"><span class="kw133">uintptr_t</span></a><span class="br0">)</span></span></span> 被分析成新式声明器，声明一个函数，它接收二个 <code>size_t</code> 和 <code>uintptr_t</code> 类型的未命名参数，而非开始定义接收二个名为“ <code>size_t</code> ”和“ <code>uintptr_t</code> ”的函数的旧式声明器。
</li><li> 参数列表可以拥有不完整类型<span class="t-rev-inl t-since-c99"><span>而且可以用 VLA 记法 [*] </span> <span><span class="t-mark-rev t-since-c99">(C99 起)</span></span></span>（除了在<a href="function_definition.html" title="c/language/function definition">函数定义</a>中，在数组到指针和函数到指针调整后，参数类型必须完整）
</li></ul>
 <table class="t-rev-begin">
<tr class="t-rev t-since-c23"><td>
<p><a href="attributes.html" title="c/language/attributes">属性说明符序列</a>亦能应用到函数参数。
</p>
</td>
<td><span class="t-mark-rev t-since-c23">(C23 起)</span></td></tr>
</table>
<p>其他函数调用机制上的细节见<a href="operator_other.html#.E5.87.BD.E6.95.B0.E8.B0.83.E7.94.A8" title="c/language/operator other">函数调用运算符</a>，关于从函数返回，见 <a href="return.html" title="c/language/return"><code>return</code></a> 。
</p>
<h3><span class="mw-headline" id=".E6.B3.A8.E8.A7.A3">注解</span></h3>
<p>不同于 C++ <span class="t-rev-inl t-since-c23"><span>与函数定义</span> <span><span class="t-mark-rev t-since-c23">(C23 起)</span></span></span>，声明器 <span class="t-c"><span class="mw-geshi c source-c">f<span class="br0">(</span><span class="br0">)</span></span></span> 与 <span class="t-c"><span class="mw-geshi c source-c">f<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span></span></span> 拥有不同含义：声明符 <span class="t-c"><span class="mw-geshi c source-c">f<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span></span></span> 是新式（原型）声明符，声明函数不接收参数。声明符 <span class="t-c"><span class="mw-geshi c source-c">f<span class="br0">(</span><span class="br0">)</span></span></span> 是声明函数接收<i>未指定</i>数量的参数的声明符（除非用于函数定义）
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw4">int</span> f<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 声明：不接收参数</span>
<span class="kw4">int</span> g<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 声明：接收未知参数</span>
 
<span class="kw4">int</span> main<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span> <span class="br0">{</span>
    f<span class="br0">(</span><span class="nu0">1</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 编译时错误</span>
    g<span class="br0">(</span><span class="nu0">2</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 未定义行为</span>
<span class="br0">}</span>
 
<span class="kw4">int</span> f<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span> <span class="br0">{</span> <span class="kw1">return</span> <span class="nu0">1</span><span class="sy4">;</span> <span class="br0">)</span> <span class="co1">// 实际定义</span>
<span class="kw4">int</span> g<span class="br0">(</span>a,b,c,d<span class="br0">)</span> <span class="kw4">int</span> a,b,c,d<span class="sy4">;</span> <span class="br0">{</span> <span class="kw1">return</span> <span class="nu0">2</span><span class="sy4">;</span> <span class="br0">}</span> <span class="co1">// 实际定义</span></pre></div></div>
<p>和在<a href="function_definition.html" title="c/language/function definition">函数定义</a>中不同，形参列表可以从 typedef 继承
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw1">typedef</span> <span class="kw4">int</span> p<span class="br0">(</span><span class="kw4">int</span> q, <span class="kw4">int</span> r<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// p 是函数类型 int(int, int)</span>
p f<span class="sy4">;</span> <span class="co1">// 声明 int f(int, int)</span></pre></div></div>
 <table class="t-rev-begin">
<tr class="t-rev t-until-c99"><td>
<p>C89 中， <span class="t-spar">说明符与限定符</span> 是可选的，且若省略它，则函数的返回类型默认为 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">int</span></span></span> （可以由 <span class="t-spar">声明符</span> 修改）。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="sy2">*</span>f<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> <span class="co1">// 返回 int* 的函数</span>
   <span class="kw1">return</span> <a href="../types/NULL.html"><span class="kw103">NULL</span></a><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
</td>
<td><span class="t-mark-rev t-until-c99">(C99 前)</span></td></tr>
</table>
<h3><span class="mw-headline" id=".E7.BC.BA.E9.99.B7.E6.8A.A5.E5.91.8A">缺陷报告</span></h3>
<p>下列更改行为的缺陷报告追溯地应用于以前出版的 C 标准。
</p>
<table class="dsctable" style="font-size:0.8em">
<tr>
<th> DR
</th>
<th> 应用于
</th>
<th> 出版时的行为
</th>
<th> 正确行为
</th></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2396.htm#dr_423">DR 423</a>
</td>
<td> C89
</td>
<td> 返回类型可以有限定
</td>
<td> 返回类型被隐式除去限定
</td></tr></table>
<h3><span class="mw-headline" id=".E5.BC.95.E7.94.A8">引用</span></h3>
<div class="t-ref-std-17">
<ul><li> C17 标准（ISO/IEC 9899:2018）：
</li></ul>
<dl><dd><ul><li> 6.7.6.3 Function declarators (including prototypes) (p: 96-98)
</li></ul>
</dd></dl>
<div class="t-ref-std-11">
<ul><li> C11 标准（ISO/IEC 9899:2011）：
</li></ul>
<dl><dd><ul><li> 6.7.6.3 Function declarators (including prototypes) (p: 133-136)
</li></ul>
</dd></dl>
<div class="t-ref-std-c99">
<ul><li> C99 标准（ISO/IEC 9899:1999）：
</li></ul>
<dl><dd><ul><li> 6.7.5.3 Function declarators (including prototypes) (p: 118-121)
</li></ul>
</dd></dl>
<div class="t-ref-std-c89">
<ul><li> C89/C90 标准（ISO/IEC 9899:1990）： 
</li></ul>
<dl><dd><ul><li> 3.5.4.3 Function declarators (including prototypes) 
</li></ul>
</dd></dl>
</div>
<h3><span class="mw-headline" id=".E5.8F.82.E9.98.85">参阅</span></h3>
<table class="t-dsc-begin">

</table></div>
</div>
</div>

<!-- 
NewPP limit report
Preprocessor visited node count: 2573/1000000
Preprocessor generated node count: 7382/1000000
Post‐expand include size: 44474/2097152 bytes
Template argument size: 11119/2097152 bytes
Highest expansion depth: 16/40
Expensive parser function count: 0/100
-->

<!-- Saved in parser cache with key mwiki1-mwiki_zh_:pcache:idhash:7970-0!*!0!!zh!*!zh!* and timestamp 20211207194803 -->
</div>                    <!-- /bodycontent -->
                                        <!-- printfooter -->
                    <div class="printfooter">
                    来自“<a href="https://zh.cppreference.com/mwiki/index.php?title=c/language/function_declaration&amp;oldid=69015">https://zh.cppreference.com/mwiki/index.php?title=c/language/function_declaration&amp;oldid=69015</a>”                    </div>
                    <!-- /printfooter -->
                                                            <!-- catlinks -->
                    <!-- /catlinks -->
                                                            <div class="visualClear"></div>
                    <!-- debughtml -->
                                        <!-- /debughtml -->
                </div>
                <!-- /bodyContent -->
            </div>
        </div>
        <!-- /content -->
        <!-- footer -->
        <div id="cpp-footer-base" class="noprint">
            <div id="footer">
                        <div id="cpp-navigation">
            <h5>导航</h5>
            <ul><li><a href="https://zh.cppreference.com/w/c/language/function_declaration">Online version</a></li><li>Offline version retrieved 2022-01-01 00:37.</li></ul></div>
                        <ul id="footer-info">
                                    <li id="footer-info-lastmod"> 本页面最后修改于2020年12月13日 (星期日) 21:28。</li>
                                    <li id="footer-info-viewcount">此页面已被浏览过5,352次。</li>
                            </ul>
                    </div>
        </div>
        <!-- /footer -->
        <script>if(window.mw){
mw.loader.state({"site":"loading","user":"missing","user.groups":"ready"});
}</script>
<script src="../../../common/skin_scripts.js"></script>
<script>if(window.mw){
mw.loader.load(["mediawiki.action.view.postEdit","mediawiki.user","mediawiki.page.ready","mediawiki.searchSuggest","mediawiki.hidpi","ext.gadget.MathJax","ext.gadget.ColiruCompiler"], null, true);
}</script>
<script src="../../../common/site_scripts.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2828341-1']);
_gaq.push(['_setDomainName', 'cppreference.com']);
_gaq.push(['_trackPageview']);
</script><!-- Served in 0.934 secs. -->
	</body>
<!-- Cached 20211207194803 -->
</html>